/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client.domainModels.NodeImpl;

import java.util.ArrayList;
import java.util.Date;

import app.model_package.EventModel;

import client.domainModels.Node.Node;

/**
 * 
 * @author Wei WANG
 */
public class Event extends AbstractNode {
	
	public static int nodeNumber = 6;
	private static ArrayList<String> history = new ArrayList<String>();
	
	public String[] getChildNodeNames() {
		String[] strings = { "Performatif", "Task", "Actor", "Receivers",
				"Conversation", "TimeStamp" };
		return strings;
	}

	public String[] getNodeAttributes() {
		// TODO Auto-generated method stub
		String[] attributes = { "ID" };
		return attributes;
	}

	public EventModel toEventModel(){
		if (verifyComplete()){
			String _task = this.getChildNodes("Task").get(0).getRep();
			String _actor = this.getChildNodes("Actor").get(0).getRep();
			String _performative = this.getChildNodes("Performatif").get(0).getRep();;
			String _conversation = this.getChildNodes("Conversation").get(0).getRep();;
			ArrayList<String> _receivers = (ArrayList<String>)((Receivers) this.getChildNodes("Receivers").get(0)).getRep1();
			
			return new EventModel("0", _task, _actor,_receivers, _performative, _conversation,new Date());
		}
		else
			return null;
		
	}
	
	public String toString() {
		return "Event: " + getID();
	}

	@Override
	public String getStringRepresentation() {
		// TODO Auto-generated method stub
		String ret = "    <event>\n";
		ret += childReader("Task");
		ret += childReader("Performatif");
		ret += childReader("Actor");
		ret += childReader("Receivers");
		ret += childReader("Conversation");
		ret += childReader("TimeStamp");
		ret += "    </event>\n";
		return ret;
	}
	
	

	
	private String childReader(String type) {
		String ret = "";
		ArrayList<AbstractNode> aux = this.getChildNodes(type);
		if (aux != null) {
			for (int i = 0; i < aux.size(); i++) {
				ret += aux.get(i).getStringRepresentation();
			}
		}
		return ret;
	}

	public boolean verifyComplete() {
		boolean b = false;
		if (getChildNames_childObjs().get("Performatif").size() > 0
				&& getChildNames_childObjs().get("Task").size() > 0
				&& getChildNames_childObjs().get("Actor").size() > 0
				&& getChildNames_childObjs().get("TimeStamp").size() > 0) {
			if ((!getChildNames_childObjs().get("Performatif").get(0).getID()
					.equals("execute"))
					&& getChildNames_childObjs().get("Receivers").size() > 0
					&& getChildNames_childObjs().get("Conversation").size() > 0) {
				if(getChildNames_childObjs().get("Receivers").get(0).getChildNames_childObjs().get("Receiver").size() > 0){
					b = true;
				}
				
			} else if (getChildNames_childObjs().get("Performatif").get(0)
					.getID().equals("execute")) {
				b = true;
			}
		}

		return b;
	}

	public void addRecordInHistory(String s){
		if(history.indexOf(s)==-1){
			history.add(s);
		}
	}
	
	public ArrayList<String> getHistoryList(){
		return history;
	}
	
	public String[] getHistorySet(){
		return (String[]) history.toArray();
	}

	@Override
	public String getRep() {
		// TODO Auto-generated method stub
		return "";
	}
}
